/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ihc.terminalbanco.gui.panels;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import ihc.terminalbanco.gui.buttons.LabelButton;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 *
 * @author Rafael
 */
public class TerminalPane extends JPanel {

    private JPanel painelPrincipal, painelNorte, painelSul, painelFirst;
    private ArrayList<JButton> buttonsList;
    private final int FIRST_COL = 1, SECOND_COL = 2, MAX_COLS_TERMINAL_BUTTON = 2;
    private int rows, cols;
    private ActionListener ActionEvents;
    private KeyListener keyEvents;
    private Font fonte, fonteOpcaoInicial = new Font("Times New Roman", Font.PLAIN, 30),
            fonteMostrarCaminho = new Font("Times New Roman", Font.PLAIN, 24);

    /**
     * Construror Principal.
     * @param name (String) Informa o nome do painel para ser mais facilmente
     * @param ActionEvents (ActionListener) Conjunto de eventos realizados pela inter-
     * face.
     * localizado.
     */
    public TerminalPane(String name, String menuNome, ActionListener ActionEvents, 
            KeyListener keyEvents, GridLayout layoutPane, Font fonte) {
        this();
        this.setLayout(new BorderLayout());
/*
        painelFirst = new JPanel(new BorderLayout());*/
        JLabel bancoLabel = new JLabel(menuNome);
        bancoLabel.setFont(this.fonteMostrarCaminho);
       // painelFirst.add(bancoLabel, BorderLayout.WEST);

        painelNorte = new JPanel(new BorderLayout());
        JLabel escolhaLabel = new JLabel("ESCOLHA A SUA OPÇÃO");
        escolhaLabel.setFont(fonteOpcaoInicial);
        escolhaLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        painelNorte.add(escolhaLabel, BorderLayout.NORTH);
        painelNorte.add(bancoLabel, BorderLayout.CENTER);
        painelPrincipal = new JPanel(layoutPane);

        //this.add(painelFirst, BorderLayout.PAGE_START);
        this.add(painelNorte, BorderLayout.NORTH);
        this.add(painelPrincipal, BorderLayout.CENTER);

        for(int cont = 0; cont < layoutPane.getRows() * layoutPane.getColumns(); cont++){
            this.buttonsList.add(null);
        }
        this.rows = layoutPane.getRows();
        this.cols = layoutPane.getColumns();

        this.setActionEvents(ActionEvents);
        this.setKeyEvents(keyEvents);
        this.setName(name);
        this.fonte = fonte;
    }

    public TerminalPane(String name, String menuNome, String textoString, ActionListener ActionEvents,
            KeyListener keyEvents, GridLayout layoutPane, Font fonte) {
        this();
        this.setLayout(new BorderLayout());

       // painelFirst = new JPanel(new BorderLayout());
        JLabel bancoLabel = new JLabel(menuNome);
        bancoLabel.setFont(this.fonteMostrarCaminho);
        //painelFirst.add(bancoLabel, BorderLayout.WEST);

        painelNorte = new JPanel(new BorderLayout());
        JLabel escolhaLabel = new JLabel("ESCOLHA A SUA OPÇÃO");
        escolhaLabel.setFont(fonteOpcaoInicial);
        escolhaLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel textoLabel = new JLabel(textoString);
        textoLabel.setFont(this.fonteMostrarCaminho);

       //textoString
        painelNorte.add(escolhaLabel, BorderLayout.NORTH);
        painelNorte.add(bancoLabel, BorderLayout.CENTER);
        painelNorte.add(textoLabel, BorderLayout.SOUTH);
        painelPrincipal = new JPanel(layoutPane);

        //this.add(painelFirst, BorderLayout.PAGE_START);
        this.add(painelNorte, BorderLayout.NORTH);
        this.add(painelPrincipal, BorderLayout.CENTER);

        for(int cont = 0; cont < layoutPane.getRows() * layoutPane.getColumns(); cont++){
            this.buttonsList.add(null);
        }
        this.rows = layoutPane.getRows();
        this.cols = layoutPane.getColumns();

        this.setActionEvents(ActionEvents);
        this.setKeyEvents(keyEvents);
        this.setName(name);
        this.fonte = fonte;
    }

    public TerminalPane(String name, ActionListener ActionEvents, KeyListener keyEvents,
            SpringLayout layoutPane, int rows, int cols) {
        this();
        this.setLayout(layoutPane);

        for(int cont = 0; cont < rows * cols; cont++){
            this.buttonsList.add(null);
        }

        this.rows = rows;
        this.cols = cols;

        this.setActionEvents(ActionEvents);
        this.setKeyEvents(keyEvents);
        this.setName(name);
    }

    public TerminalPane() {
        this.buttonsList = new ArrayList<JButton>();
    }

    public TerminalPane(boolean isDoubleBuffered) {
        super(isDoubleBuffered);
    }

    public TerminalPane(LayoutManager layout) {
        super(layout);
    }

    public TerminalPane(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
    }

    public void addLabelButtonToTheList(LabelButton newButton) {
        int buttonNumber = generateButtonNumber(newButton.getRow(), newButton.getCol(), this.cols, this.rows);
        String textOfButton = buttonNumber + " " + newButton.getName();
        //System.out.println(buttonNumber + " " + newButton.getName());
        newButton.setText(textOfButton);
        newButton.addKeyListener(keyEvents);
        newButton.addActionListener(this.ActionEvents);
        newButton.setFont(this.fonte);

        if (this.cols == 4) {
            int buttonPosition = generateButtonPosition(newButton.getRow(),
                    newButton.getCol() + 1, this.cols);
            this.buttonsList.set(buttonPosition, newButton);
            if (newButton.getCol() == FIRST_COL) {
                createAndPrepareTerminalButton(buttonNumber, textOfButton,
                        newButton.getRow(), newButton.getCol(), this.cols);
            } else if (newButton.getCol() == SECOND_COL) {
                createAndPrepareTerminalButton(buttonNumber, textOfButton,
                        newButton.getRow(), newButton.getCol() + 2, this.cols);
            }
        } else if (this.cols == 2) {
            int buttonPosition = generateButtonPosition(newButton.getRow(),
                    newButton.getCol(), this.cols);
            this.buttonsList.set(buttonPosition, newButton);
        }

    }

    public void addLabelButtonList(List<LabelButton> newButton) {
        for (LabelButton labelButton : newButton) {
            this.addLabelButtonToTheList(labelButton);
        }
        addButtonsList();
    }

    public int generateButtonPosition(int row, int col, int numberOfCols) {
        return (col - 1) + (numberOfCols * (row - 1));
    }

    public int generateButtonNumber(int row, int col, int numberOfCols, int numberOfRows) {
        
        return ( row + numberOfRows * (col-1));

    }

    private void createAndPrepareTerminalButton(int buttonNumber, String textOfButton,
            int row, int col, int numberOfCols) {
        JButton terminalButton = new JButton(Integer.toString(buttonNumber));
        terminalButton.addActionListener(this.ActionEvents);
        terminalButton.setName(textOfButton);

        Font font = LabelButton.getStaticFont();
        terminalButton.setFont(font);

        int buttonPosition = generateButtonPosition(row, col, numberOfCols);
        this.buttonsList.set(buttonPosition, terminalButton);
    }

    private void addButtonsList() {
        for (JButton jButton : buttonsList) {
            if(jButton != null){
            painelPrincipal.add(jButton);
            }
        }
    }

    public ActionListener getActionEvents() {
        return ActionEvents;
    }

    public void setActionEvents(ActionListener events) {
        this.ActionEvents = events;
    }

    public KeyListener getKeyEvents() {
        return keyEvents;
    }

    public void setKeyEvents(KeyListener keyEvents) {
        this.keyEvents = keyEvents;
    }
}

